package com.example.androidnote.Util;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.androidnote.R;

import org.w3c.dom.Text;

public class ComplexAdapter extends BaseAdapter {
    private Context context;
    private Cursor cursor;
    private RelativeLayout layout;
    public ComplexAdapter(Context context, Cursor cursor){
        this.context = context;
        this.cursor = cursor;
    }
    @Override
    public int getCount() {
        return cursor.getCount();
    }
    @Override
    public Object getItem(int position) {
        return cursor.getPosition();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        layout = (RelativeLayout) inflater.inflate(R.layout.list_layout,null);
        TextView tv = layout.findViewById(R.id.text1);
        TextView time = layout.findViewById(R.id.time1);
        ImageView img = layout.findViewById(R.id.img1);
        ImageView vedio = layout.findViewById(R.id.ved1);
        cursor.moveToPosition(position);
        String content = cursor.getString(cursor.getColumnIndex(NoteDB.CONTENT));
        String time1 = cursor.getString(cursor.getColumnIndex(NoteDB.TIME));
        String uri = cursor.getString(cursor.getColumnIndex(NoteDB.IMG_PATH));
        String url = cursor.getString(cursor.getColumnIndex(NoteDB.VED_PATH));
        tv.setText(content);
        time.setText(time1);
        if (!uri.equals("error")){
            img.setImageBitmap(DBUtils.getImg(uri,200,200));
        }
        if (!url.equals("error")){
            vedio.setImageBitmap(DBUtils.getVideoImg(url,200,200, MediaStore.Images.Thumbnails.MICRO_KIND));
        }
        return layout;
    }
}
